<?php
/**
 * Created by PhpStorm
 * User: whh
 * Date: 2022/7/23
 * Time: 11:53
 */

namespace app\common\model;

use think\Db;
use think\Exception;
use think\Model;

class NovelChapter extends Model
{
    use Common;

    protected $table = 'novel_chapter';
    public $contentTable = '';


    /**
     * 获取章节内容
     * @param int $novelId
     * @param int $chapterNo
     * @return array|\PDOStatement|string|Model|null
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getContent($novelId = 0, $chapterNo = 0)
    {
        if (!$novelId && (!isset($this->novel_id) || !$this->novel_id)) {
            throw new Exception('章节模型未携带书籍ID');
        }
        if (!$chapterNo && (!isset($this->chapter_no) || !$this->chapter_no)) {
            throw new Exception('章节模型未携带章节序号');
        }
        $novelId = $novelId ? $novelId : $this->novel_id;
        $chapterNo = $chapterNo ? $chapterNo : $this->chapter_no;
        $this->contentTable = "novel_chapter_content_" . intval(substr($novelId, -2));
        $where[] = ['novel_id', '=', $novelId];
        $where[] = ['chapter_no', '=', $chapterNo];
        return Db::table($this->contentTable)->where($where)->cache(1800)->find();
    }


    /**
     * 获取章节内容
     * @param int $novelId
     * @param int $chapterNo
     * @return array|\PDOStatement|string|Model|null
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getHtContent($novelId = 0, $chapterNo = 0)
    {
        if (!$novelId && (!isset($this->novel_id) || !$this->novel_id)) {
            throw new Exception('章节模型未携带书籍ID');
        }
        if (!$chapterNo && (!isset($this->chapter_no) || !$this->chapter_no)) {
            throw new Exception('章节模型未携带章节序号');
        }
        $novelId = $novelId ? $novelId : $this->novel_id;
        $chapterNo = $chapterNo ? $chapterNo : $this->chapter_no;
        $this->contentTable = "novel_chapter_content_" . intval(substr($novelId, -2));
        $where[] = ['novel_id', '=', $novelId];
        $where[] = ['chapter_no', '=', $chapterNo];
        return Db::table($this->contentTable)->where($where)->find();
    }

    /**
     * 设置内容
     * @param $content
     * @param int $novelId
     * @param int $chapterNo
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function setContent($content, $novelId = 0, $chapterNo = 0)
    {
        if (!$novelId && (!isset($this->novel_id) || !$this->novel_id)) {
            throw new Exception('章节模型未携带书籍ID');
        }
        if (!$chapterNo && (!isset($this->chapter_no) || !$this->chapter_no)) {
            throw new Exception('章节模型未携带章节序号');
        }
        $novelId = $novelId ? $novelId : $this->novel_id;
        $chapterNo = $chapterNo ? $chapterNo : $this->chapter_no;
        $this->contentTable = "novel_chapter_content_" . intval(substr($novelId, -2));
        $where[] = ['novel_id', '=', $novelId];
        $where[] = ['chapter_no', '=', $chapterNo];
        $save['novel_id'] = $novelId;
        $save['chapter_no'] = $chapterNo;
        $save['content'] = $content;
        $have = Db::table($this->contentTable)->where($where)->find();
        if ($have) {
            Db::table($this->contentTable)->where($where)->update($save);
        } else {
            Db::table($this->contentTable)->insert($save);
        }
    }


}